package com.ids.transfer.controler;

import java.util.HashMap;

import com.ids.transfer.AbstractExtraTask;
import com.ids.transfer.ExtraFactory;
import com.ids.transfer.ExtraParam;
import com.ids.transfer.IExtra;
import com.ids.transfer.config.DatasourceConfig;
import com.ids.transfer.config.ItemConfig;
import com.ids.transfer.config.TaskConfig;
import com.ids.transfer.data.XmlParse;

/**
 * Auto Task Class
 * 初始化参数：数据源、实现类、任务
 * as the Handler
 */
public class ExtraTask extends AbstractExtraTask {
	
	private ExtraParam ep;

	/**
	 * 禁用无参构造
	 */
	private ExtraTask() {
	}

	/**
	 * 根据指定taskid初始化所有Item放到tasks集合里,
	 * 执行一次只存一个task
	 * @param taskid
	 */
	public ExtraTask(String taskid) {
		//初始化参数
		ep = super.readXmlInfo(taskid);
		if (ep == null) {
			System.out.println("xml parser error,system exit(0)");
			System.exit(0);
		}
		//取出所有类
		HashMap<String,ItemConfig> items = (HashMap<String, ItemConfig>) ep.getValue(ExtraParam.ITEMS);

		//创建所有Iextra加到集合里
		HashMap<String,IExtra> itemMap = new HashMap<String,IExtra>();
		for (String itemName : items.keySet()) {
			//一个类对应一个初始化参数
			IExtra ie = (IExtra) ExtraFactory.createEtra(itemName);
			ie.setParams(ep);
			ie.setName(itemName);
			//ie.extra();
			itemMap.put(itemName, ie);
		}
		//根据不同的taskID加入task下的Items,已初始化
		ep.addParam(taskid, itemMap);
	}


	@Override
	public void execute() {
		// TODO Auto-generated method stub
		TaskConfig tc= (TaskConfig) ep.getValue(ExtraParam.TASKCONFIG);
		HashMap<String,IExtra> items = (HashMap<String, IExtra>) ep.getValue(tc.getId());;
		for (String key : items.keySet()) {
			items.get(key).extra();
		}
	}

	/**
	 * 往本类加入参数,参数private
	 * @param ep
	 */
	public void setExtraParam(ExtraParam ep) {
		if (this.ep == null)
			this.ep = ep;
		else {
			this.ep.addParam(ep);
		}
	}

	/**
	 * 取得本类的参数
	 * @return
	 */
	public ExtraParam getExtraParam() {
		return ep;
	}

	public static void main(String[] args) {
		//AutoTransfer at = new AutoTransfer("executeTask");
		ExtraTask at = new ExtraTask("genTask");
		at.execute();
	}
}